﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASBCC.Database.Entity;
using ASBCC.Database.Service;
using BEGDC.CC.Web.Workflow;

namespace BEGDC.CC.Web.View
{
    public partial class ProcessDelegation : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                UserDelegationTableEntitySet set1 = UserDelegationManager.SelectByCondition(string.Format("UserID = {0}", UserInfo.EmployeeID));
                if (set1.Count > 0)
                {
                    //hidGuid1.Value = set1[0].UserDelegationID.ToString();
                    txtDateFrom.Text = set1[0].FromDate + "";
                    txtDateTo.Text = set1[0].ToDate + "";

                    EmployeeTableEntitySet set2 = EmployeeManager.SelectByCondition(string.Format("EmployeeID = {0}", set1[0].DelegateTo));
                    if (set2.Count > 0)
                    {
                        ss.EmployeeID = set2[0].EmployeeNo;
                        ss.EmployeeName = set2[0].NameEN;
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            //List<string> errors = new List<string>();

            //if (errors.Count > 0)
            //{
            //    Page.RegisterClientScriptBlock("", string.Format("<script>alert('{0}');</script>",
            //        string.Join("\\n", errors.ToArray())));
            //    return;
            //}

            if (!string.IsNullOrEmpty(ss.EmployeeID))
            {
                EmployeeTableEntity ent1 = EmployeeManager.SelectByKeys(ss.EmployeeID);
                if (ent1 != null && ent1.EmployeeID != null)
                {
                    UserDelegationTableEntitySet set1 = UserDelegationManager.SelectByCondition(string.Format("UserID = {0}", UserInfo.EmployeeID));
                    if (set1.Count > 0)
                    {
                        set1[0].DelegateTo = ent1.EmployeeID;
                        if (!string.IsNullOrEmpty(txtDateFrom.Text))
                        {
                            set1[0].FromDate = DateTime.Parse(txtDateFrom.Text);
                        }
                        else
                        {
                            set1[0].FromDate = null;
                        }
                        if (!string.IsNullOrEmpty(txtDateTo.Text))
                        {
                            set1[0].ToDate = DateTime.Parse(txtDateTo.Text);
                        }
                        else
                        {
                            set1[0].ToDate = null;
                        }
                        UserDelegationManager.Update(set1[0]);
                    }
                    else
                    {
                        UserDelegationTableEntity proxy1 = new UserDelegationTableEntity();
                        proxy1.UserDelegationID = Guid.NewGuid();
                        proxy1.CategoryName = "EZVisit";
                        //proxy1.Disabled = "false";
                        proxy1.DelegateTo = ent1.EmployeeID;
                        if (!string.IsNullOrEmpty(txtDateFrom.Text))
                        {
                            proxy1.FromDate = DateTime.Parse(txtDateFrom.Text);
                        }
                        if (!string.IsNullOrEmpty(txtDateTo.Text))
                        {
                            proxy1.ToDate = DateTime.Parse(txtDateTo.Text);
                        }
                        proxy1.UserID = UserInfo.EmployeeID;
                        UserDelegationManager.Insert(proxy1);

                        //hidGuid1.Value = proxy1.UserDelegationID.ToString();
                    }
                }
            }

            Page.RegisterClientScriptBlock("", "<script>alert('Delegation saved successfully');window.close();window.returnValue='reload';</script>");
        }
    }
}